package top.yokey.android.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.fragment.MineFragment;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class ModifyNickNameActivity extends BaseActivity {

    private AppCompatImageView backImageView;
    private AppCompatEditText newPasswordEditText;
    private AppCompatTextView modifyTextView;
    private View oldPasswordView, newPasswordView;
    private Context context = this;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_nickname);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        newPasswordEditText = findViewById(R.id.newPasswordEditText);
        modifyTextView = findViewById(R.id.modifyTextView);
        oldPasswordView = findViewById(R.id.oldPasswordView);
        newPasswordView = findViewById(R.id.newPasswordView);

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> finish());

        newPasswordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                oldPasswordView.setBackgroundResource(R.color.textHint);
                newPasswordView.setBackgroundResource(R.color.primary);
            }
        });

        modifyTextView.setOnClickListener(view -> {
            String newPass = newPasswordEditText.getText().toString();

            if (newPass.isEmpty()) {
                Toast.makeText(this, "请输入新昵称", Toast.LENGTH_SHORT).show();
                return;
            }
            modifyTextView.setEnabled(false);//禁用按钮
            modifyTextView.setText("正在修改...");//修改按钮文字


            HttpHelp.get().ready("member", "modifyNickname")
                    .add("nickname", newPass)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(ModifyNickNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(ModifyNickNameActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);//启用按钮
                            modifyTextView.setText("修改昵称");//修改按钮文字
                        }
                    });
        });

    }

}
